﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace JqueryNet.FormControl.FormWebControl
{
    public class JCheckBoxList : JListControl//JWebControl
    {
        CheckBoxList checkboxlist;

        public JCheckBoxList(CheckBoxList checkboxlist)
            : base(checkboxlist)
        {
            this.checkboxlist = checkboxlist;            
        }

        public override void Attr(string key)
        {
            switch (key.ToLower())
            {
                case "backcolor":
                    Value = checkboxlist.BackColor.Name;
                    return;
                case "bordercolor":
                    Value = checkboxlist.BorderColor.Name;
                    return;
                case "borderwidth":
                    Value = checkboxlist.BorderWidth;
                    return;
                case "cellpadding":
                    Value = checkboxlist.CellPadding;
                    return;
                case "cellspacing":
                    Value = checkboxlist.CellSpacing;
                    return;
                case "css":
                case "cssclass":
                case "class":
                    Value = checkboxlist.CssClass;
                    return;
                case "items":
                    Value = checkboxlist.Items;
                    return;
                case "selectitem":
                    Value = checkboxlist.SelectedItem;
                    return;
                case "selectvalue":
                    Value = checkboxlist.SelectedValue;
                    return;
                case "text":
                    Value = checkboxlist.Text;
                    return;
                case "tooltip":
                case "title":
                    Value = checkboxlist.ToolTip;
                    return;
                case "selectedindex":
                case "index":
                    Value = checkboxlist.SelectedIndex; return;
                case "width":
                    Value = checkboxlist.Width;
                    return;
                default:
                    base.Attr(key); return;
            }

        }

        public override void Attr(string key, string value)
        {
            int b;
            int.TryParse(value, out b);
            switch (key.ToLower())
            {
                case "borderwidth":
                    checkboxlist.BorderWidth = b;
                    return;
                case "cellpadding":
                    checkboxlist.CellPadding = b;
                    return;
                case "cellspacing":
                    checkboxlist.CellSpacing = b;
                    return;
                case "css":
                case "cssclass":
                case "class":
                    checkboxlist.CssClass = value;
                    return;
                case "selectvalue":
                    checkboxlist.SelectedValue = value;
                    return;
                case "text":
                    checkboxlist.Text = value;
                    return;
                case "tooltip":
                case "title":
                    checkboxlist.ToolTip = value;
                    return;
                case "selectedindex":
                case "index":
                    checkboxlist.SelectedIndex = b;
                    return;
                case "width":
                    checkboxlist.Width = b;
                    return;
                default:
                    base.Attr(key, value); return;
            }

        }

        public override void Val()
        {
            List<ListItem> list = new List<ListItem>();
            foreach (ListItem item in checkboxlist.Items)
            {
                if (item.Selected)
                {
                    list.Add(item);                    
                }
            }
            Value = list.ToArray();
        }

        public override void Val(string value)
        {
            checkboxlist.SelectedValue = value;
        }

        public override void Val(string[] values)
        {
            if (values == null || values.Count() == 0) return;
            foreach (ListItem item in checkboxlist.Items)
            {
                if (values.Contains(item.Value))
                {
                    item.Selected = true;
                }
            }
        }
    }
}
